var name = window.localStorage.getItem('name')
var userInfo = window.localStorage.getItem('userInfo')
userInfo = JSON.parse(userInfo)
console.log(userInfo);
$('[name="username"]').val(name)
$('[name="nickname"]').val(userInfo.nickname)
$('[name="sex"]').val(userInfo.sex)
$('[name="age"]').val(userInfo.age)
$('[name="identity"]').val(userInfo.identity)

$('form').on('submit', function () {
    var nickname = $('[name="nickname"]').val()
    var sex = $('[name="sex"]').val()
    var age = $('[name="age"]').val()
    var identity = $('[name="identity"]').val()

    var xhr = new XMLHttpRequest;
    xhr.open('post', '/api/info');
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded')
    xhr.send(`username=${name}&nickname=${nickname}&sex=${sex}&age=${age}&identity=${identity}`)
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status >= 200 && xhr.status < 300) {
                var res = xhr.responseText;
                res = JSON.parse(res);
                console.log(res);
                if(res.code === 1){
                    layer.msg(res.msg,{
                        icon:1,
                        time:1000
                    },()=>{
                        window.localStorage.removeItem('name')
                        window.localStorage.removeItem('userInfo')
                        location.href = '../login.html'
                    })
                }
            }
        }
    }
    //阻止默认行为
    return false
})